﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Data.SqlClient;


namespace SuperSign_SocialMedia
{
    public partial class Form1 : Form
    {
        const string m_FEEDADDRESS = @"http://www.undercovercolor.com/feed";
        string m_ConnectionString = @"Data Source=SQL01;Initial Catalog=SuperSign;Persist Security Info=True;User ID=sa ; Password=sa";
        bool m_ThreadGO = true;

        public Form1()
        {
            InitializeComponent();
            Bt_Stop.Enabled = false;
        }

        private void Bt_Start_Click(object sender, EventArgs e)
        {
            m_ThreadGO = true;

            Thread feedThread = new Thread(new ThreadStart(fetchingFeed));
            feedThread.Start();
            Bt_Start.Enabled = false;
            Bt_Stop.Enabled = true;
        }

        private void fetchingFeed()
        {
            while (m_ThreadGO)
            {
                string Text_Color = "[Color Val=\"#030303\"]";
                string Text_Font = "[Font Val=\"Gotham Light\"]";
                string Author_Font = "[Font Val=\"Gotham Book\"]";
                string ImageTag_ID = "[IMAGETAG ID=\"823\"]";
                string BG_Color;

                XmlDocument XML_SignFeed = new XmlDocument();
                XML_SignFeed.Load(m_FEEDADDRESS);

                XmlNode Node_Color = XML_SignFeed.SelectSingleNode("//ABCSuperSign/Color");
                string ColorSetID = Node_Color.Attributes["ColorSetID"].Value;

                XmlNode Node_TweetsCollection = XML_SignFeed.SelectSingleNode("//ABCSuperSign/Tweets");
                string ColorTweetBG = Node_TweetsCollection.Attributes["Background"].Value;
                BG_Color = "[bgColor Val=\"" + ColorTweetBG + "\"]";
                

                XmlNodeList Node_Tweets = Node_TweetsCollection.ChildNodes;

                string Txt_7thRibbon = Text_Color;

                int Count_Tweet = 0;
                foreach (XmlNode Node_Tweet in Node_Tweets)
                {
                   // Count_Tweet++;
                    Txt_7thRibbon += ImageTag_ID
                        + Author_Font + "@" + Node_Tweet.SelectSingleNode("//Author").InnerText
                        + " "
                        + Text_Font + Node_Tweet.SelectSingleNode("//Text").InnerText
                        + "   ";
                    //if (Count_Tweet > 5)
                    //    break;
                }

                UpdateToDB(Txt_7thRibbon, "[Hanes_Twitter]");
                UpdateToDB(ColorSetID, "[Hanes_Color]");

                Thread.Sleep(30000);
            }
        }

        private void UpdateToDB(string dataTagInfo, string TageName)
        {
            SqlConnection conn = new SqlConnection(m_ConnectionString);
            try
            {
                conn.Open();

                string sqlcmd = "update DataTag set LastValue = '" + dataTagInfo.Replace("'", "''") + "' where Name = '" + TageName + "'";

                SqlCommand cmd = new SqlCommand(sqlcmd, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
            }
            conn.Close();
        }


        private void Bt_Stop_Click(object sender, EventArgs e)
        {
            m_ThreadGO = false;
        }

    }
}
